package com.exmaple.baidumv;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.DownloadListener;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.baidumv.R;
import com.exmaple.baidumv.comm.App;

public class BaseWebViewActivity extends Activity {
	private ProgressBar progressBar;
	private WebView webview;
	private ImageView back;
	private TextView tv_title;
	private String url;
	private String title;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(
				App.getIdByName(this, "layout", "dm_base_webview_main"), null);
		init();
		return view;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.dm_base_webview_main);
		init();
		super.onCreate(savedInstanceState);
	}

	private void init() {
		title = getIntent().getStringExtra("title");
		url = getIntent().getStringExtra("url");
		progressBar = (ProgressBar) this.findViewById(App.getIdByName(this,
				"id", "dm_base_webview_progressBar"));
		webview = (WebView) this.findViewById(App.getIdByName(this, "id",
				"dm_base_webview_webview"));
		back = (ImageView) this.findViewById(App.getIdByName(this, "id",
				"dm_base_webview_img_back"));
		tv_title = (TextView) this.findViewById(App.getIdByName(this, "id",
				"dm_base_webview_tv_title"));
		tv_title.setText(title);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		setSettings(webview);
		webview.loadUrl(url);
		webview.setDownloadListener(new MyWebViewDownLoadListener());
		webview.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onReceivedTitle(WebView view, String text) {
				super.onReceivedTitle(view, text);
			}

			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				if (newProgress == 100) {
					progressBar.setVisibility(View.GONE);
				} else {
					if (progressBar.getVisibility() == View.GONE)
						progressBar.setVisibility(View.VISIBLE);
					progressBar.setProgress(newProgress);
				}
				super.onProgressChanged(view, newProgress);
			}

			@Override
			public void onShowCustomView(View view, CustomViewCallback callback) {
				super.onShowCustomView(view, callback);
			}
		});

		webview.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				if (url.startsWith("tel:") || url.startsWith("sms:")
						|| url.startsWith("weixin://")) {
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri
							.parse(url));
					startActivity(intent);
					return true;
				}
				return false;
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
			}

			@Override
			public void onReceivedSslError(WebView view,
					SslErrorHandler handler, SslError error) {
				super.onReceivedSslError(view, handler, error);
				handler.cancel();
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				if (!webview.getSettings().getLoadsImagesAutomatically()) {
					webview.getSettings().setLoadsImagesAutomatically(true);
				}
				super.onPageFinished(view, url);
			}

			public void onFormResubmission(WebView view, Message dontResend,
					Message resend) {
				resend.sendToTarget();
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (webview.canGoBack() && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			webview.goBack();
			return true;
		}
		return false;
	}

	@Override
	public void onPause() {
		webview.onPause();
		super.onPause();
	}

	@Override
	public void onResume() {
		webview.onResume();
		super.onResume();
	}

	private void setSettings(WebView webview) {
		webview.getSettings().setJavaScriptEnabled(true);
		webview.getSettings().setAllowFileAccess(true);
		webview.getSettings().setDefaultTextEncodingName("UTF-8");
		webview.getSettings().setSupportZoom(true);
		// webview.getSettings().setUserAgentString("User-Agent");
		webview.getSettings().setAppCacheEnabled(true);
		webview.getSettings().setDomStorageEnabled(true);
		webview.getSettings().setLoadWithOverviewMode(true);
		webview.getSettings().setUseWideViewPort(true);
		webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); // 默认使用缓存
		webview.getSettings().setPluginState(PluginState.ON);
		webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		webview.getSettings().setBuiltInZoomControls(false);
		webview.setHorizontalScrollBarEnabled(false);// 水平不显示
		if (Build.VERSION.SDK_INT >= 19) {
			webview.getSettings().setLoadsImagesAutomatically(true);
		} else {
			webview.getSettings().setLoadsImagesAutomatically(false);
		}
	}

	private class MyWebViewDownLoadListener implements DownloadListener {
		@Override
		public void onDownloadStart(String url, String userAgent,
				String contentDisposition, String mimetype, long contentLength) {
			Uri uri = Uri.parse(url);
			Intent intent = new Intent(Intent.ACTION_VIEW, uri);
			startActivity(intent);
		}
	}
}
